home *** CD-ROM | disk | FTP | other *** search
- Path: cs.tu-berlin.de!news
- From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
- Newsgroups: comp.lang.c++
- Subject: Re: Overloaded Virtual functions
- Date: 28 Feb 1996 02:42:29 GMT
- Organization: Technical University of Berlin, Germany
- Message-ID: <4h0fel$pru@news.cs.tu-berlin.de>
- NNTP-Posting-Host: 130.149.17.236
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
-
- Kevin Ingalls <ingkl900@ccmail.ca.boeing.com> writes:
- > sonia_mangat@bdsi.com (Sonia Mangat) wrote:
- > >Hi everybody,
- > >I have a question on overloaded virtual functions. I have included a
- > >program that has a parent class with overloaded virtual functions.
- > >I have also attached the compilation error I get.
- > >
- > >The ways I can think of correcting this problem are
- > > --> Declare the other prototype in the class B and make it call it's
- > > parent's function.
- > > --> Give different names for the virtual function.
- > >
- > > I don't like both these options. Is there any other way to solve this
- >
- > >problem?
- > >
- > >Please respond to : vidhya@bdsi.com
- > >
- > >Thanks in advance
- > >vidhya
- >
- > I think that you have been misled about virtual functions. In order to make
- > use of virtual functions, you need pointers to base classes. The virtual function
- > mechanism does not work if you use objects directly. I have reworked your code
- > example to make use of virtual functions. I hope that this helps. Let me know
- > if you need clarification.
-
- In this case, the problem is not the virtual function mechanism. Actually, it does
- work: if you declare B b and then call b.print(0) B::print is called and this is
- what virtual functions are all about ( the mechanism isn't used in this particular
- case, but the result is the same ). The point is that when you declare a base class
- with a set of overloaded virtual functions and reimplement one of these functions
- in a derived class ALL base class functions with the same name are hidden by the
- reimplemented function. That means you need a qualifier: simply replace b.print(p)
- by b.A::print(p), i.e. tell the compiler to call A's function.
- I think this is a serious weakness of the language since the unique identifier of
- a function is ( or should be ) its name and the types of its arguments. Thus, the
- functions print(int) and print(const& T) shouldn't be related at all. You won't
- expect a function named print to hide a function named read, would you?
-
- Bye
-
- Roman
-
-
-